package Jdbc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import Model.Editor;
import Model.Hakem;
import Model.Profil;
import Model.Rol;
import Model.Yazar;
import Model.Uye;


public class JdbcSelen {
	
	
	static Connection con = null;
	static Statement st = null;
	static ResultSet rs = null;

    static public void Connect(){
    		
		    String url = "jdbc:postgresql://localhost:5432/yazgellab";
		    String user = "postgres";
		    String password = "12345";
		
		    try {
		    	Class.forName("org.postgresql.Driver");
		        con = DriverManager.getConnection(url, user, password);
		
		    } catch (Exception ex) {
		      System.out.println("baglanilamadi");
		
		    }
      }

    static public void close(){
	
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            System.out.println("Kapatilamadi");
        }
}
    
 
	
	private static void setSession(String key, Object obj) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute(key, obj);
	}
	
	public Object getSession(String key) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		return session.getAttribute(key);
	}

	static public void profilDuzenle(Profil profil, int uye_pk){
	
		Connect();
		try {
		
			st = con.createStatement();
			String query = "UPDATE profil SET ad ='"+profil.getAd()+"',soyad='"+profil.getSoyad()+"',dogum_yeri='"+profil.getDogum_yeri()+"',dogum_tarihi='"+profil.getDogum_tarihi()+"',meslek='"+profil.getMeslek()+"',uzmanlik='"+profil.getUzmanlik_alani()+"',unvan='"+profil.getUnvan()+"',ozgecmis='"+profil.getOz_gecmis()+"' WHERE uye_pk ='"+uye_pk+"';";
			st.executeUpdate(query);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		finally{
		close();
		}
	}

	static public void silinmeBasvurusuYap(int uye_id_silinme_basvurusu)
	{
		Connect();
		try{
			st = con.createStatement();
			String uye_id_sil_bas = "INSERT INTO silinme_basvurusu (uye_pk) VALUES ("+uye_id_silinme_basvurusu+")";
			st.executeUpdate(uye_id_sil_bas);
		}catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			close();
		}
}
	
	static public void rolBasvurusuYap(int uye_id_rol_basvurusu, int rol_secimi)
	{
		Connect();
		try{
			st = con.createStatement();
			String rol_basvurumu_yaptim = "INSERT INTO rol_basvurusu (uye_pk,rol) VALUES ("+uye_id_rol_basvurusu+","+rol_secimi+");";
			st.executeUpdate(rol_basvurumu_yaptim);
		}catch (SQLException e) {
			e.printStackTrace();
		}	
		finally{
			close();
		}
}

	public String listele() {
		Connect();
		String basliklar="";
		try{
			st = con.createStatement();
			rs = st.executeQuery("select makale_pk,baslik,makale_yolu,yayinlanma_yili,ad,soyad from makale join yazar on (makale.yazar_pk=yazar.yazar_pk) join profil on(yazar.uye_pk=profil.uye_pk);");
		
			while(rs.next())
			{
				basliklar+=rs.getString(1)+"|"+rs.getString(2)+"|"+rs.getString(3)+"|"+rs.getString(4)+"|"+rs.getString(5)+" "+rs.getString(6)+"|";
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
		
		finally{
			close();
		}
		return basliklar;
}
	
	 public static void yorumYap(String tarih , String yorum, int yorum_makale_id, int yorum_uye_id)
	 {
		Connect();
		try{
			st = con.createStatement();
			String yorum_yaptim= "INSERT INTO yorum(tarih,yorum, makale_pk,uye_pk) VALUES ('"+tarih+"','"+yorum+"','"+yorum_makale_id+"','"+yorum_uye_id+"');";
			st.executeUpdate(yorum_yaptim);
		}catch (SQLException e) {
			e.printStackTrace();
		}	
		finally{
			close();
		}
	 }
}



	
	

